var path=$("#pathId").val();
$('#old_code').click(function(){
    var flag=true;
    var msg="";
    var old_mobile = $('#old_mobile').val();
    if(old_mobile==null||old_mobile==""){
        msg="原手机号不能为空！";
        flag=false;
    }else if(!/^1[34578]\d{9}$/.test(old_mobile)){
        msg="原手机号格式不正确！";
        flag=false;
    }else if(old_mobile!=login_phone){
        msg="该号码不是当前登录手机号码！";
        flag=false;
    }
    if(flag){
        var $this = $(this),
            _this = this,
            i = 60;
        var old_mobile = $('#old_mobile').val();
        $.ajax({
            url: path +"/aem/staffPropertyInfo/ajaxSmsSend",
            type: "post",
            async: true,
            data:{phone:old_mobile,codeType:"change_code",cid:cid,type:0},
            dataType: "json",
            timeout: 1000,
            error: function(){},
            success: function(data){
                if(data.status == "40001"){
                    $("#new-error").attr("style","display:block;color:red;");
                    $("#new-error").html(data.message);
                }else{
                    _this.disabled = 'disabled';
                    clearInterval(repeat);
                    var repeat = setInterval(function(){
                        i--;
                        if(i == 0){
                            _this.disabled = false;
                            $this.css('color','#ff892f');
                            $this.html('获取验证码');
                            clearInterval(repeat);
                        }else{
                            $this.html(i+'s后重新获取');
                            $this.css('color','#efefef');
                        }
                    },1000);

                    $("#new-error").attr("style","display:none;");
                }
            }
        });
    }else{
        $("#new-error").attr("style","display:block;color:red;");
        $("#new-error").html(msg);
    }
});

$('#new_code').click(function(){
    var flag=true;
    var msg="";
    var new_mobile = $('#new_mobile').val();
    if(new_mobile==null||new_mobile==""){
        msg="新手机号不能为空！";
        flag=false;
    }else if(!/^1[34578]\d{9}$/.test(new_mobile)){
        msg="新手机号格式不正确！";
        flag=false;
    }else if(new_mobile==login_phone){
        msg="更改手机号码不能为当前登录手机号码！";
        flag=false;
    }
    if(flag){
        $.ajax({
            url:path +"/aem/staffPropertyInfo/checkPhoneExist",
            type: "post",
            async: false,
            data:{"cid":cid,"phone":new_mobile},
            cache:true,
            dataType: "json",
            timeout: 1000,
            error: function(){
                flag=false;
            },
            success: function(data){
                if (data.status == "301"){
                    msg="该手机号已注册，请重新输入。";
                    flag=false;
                }
            }
        });
    }
    if(flag){
        var $this = $(this),
            _this = this,
            i = 60;
        var new_mobile = $('#new_mobile').val();
        $.ajax({
            url: path +"/aem/staffPropertyInfo/ajaxSmsSend",
            type: "post",
            async: true,
            data:{phone:new_mobile,codeType:"change_code",cid:cid,type:1},
            dataType: "json",
            timeout: 1000,
            error: function(){},
            success: function(data){
                if(data.status == "40001"){
                    $("#new-error").attr("style","display:block;color:red;");
                    $("#new-error").html(data.message);
                }else{
                    _this.disabled = 'disabled';
                    clearInterval(repeat);
                    var repeat = setInterval(function(){
                        i--;
                        if(i == 0){
                            _this.disabled = false;
                            $this.css('color','#ffbe39');
                            $this.html('获取验证码');
                            clearInterval(repeat);
                        }else{
                            $this.html(i+'s后重新获取');
                            $this.css('color','#efefef');
                        }
                    },1000);

                    $("#new-error").attr("style","display:none;");
                }
            }
        });
    }else{
        $("#new-error").attr("style","display:block;color:red;");
        $("#new-error").html(msg);
    }
});

$('#checkSms').click(function(){
    //alert("验证进入");
    var flag=true;
    var msg="";
    var old_mobile = $('#old_mobile').val();
    var new_mobile = $('#new_mobile').val();
    var old_ver_code = $('#old_ver_code').val();
    var new_ver_code = $('#new_ver_code').val();
    if(old_mobile==null||old_mobile==""){
        msg="原手机号不能为空！";
        flag=false;
    }else if(new_mobile==null||new_mobile==""){
        msg="新手机号不能为空！";
        flag=false;
    }else if(!/^1[34578]\d{9}$/.test(old_mobile) || !/^1[34578]\d{9}$/.test(new_mobile)){
        msg="手机号格式不正确！";
        flag=false;
    }
    if(flag){
        if (old_ver_code==null||old_ver_code=="") {
            msg="原手机验证码不能为空！";
            flag=false;
        }else if(new_ver_code==null||new_ver_code==""){
            msg="新手机验证码不能为空！";
            flag=false;
        }
    }
    if(flag){
        $.ajax({
            url: path+"/aem/staffPropertyInfo/ajaxSmsVerify",
            type: "post",
            async: false,
            data:{"account":old_mobile,"verCode":old_ver_code,"phone":new_mobile,"newVerCode":new_ver_code,"codeType":"change_code"},
            cache:false,
            dataType: "json",
            timeout: 1000,
            error: function(){},
            success: function(data){
                if (data.status != "200"){
                    msg="验证码输入错误！";
                    flag=false;
                }
            }
        });
    }
    if(flag){
        $.ajax({
            url: path+"/aem/staffPropertyInfo/updateStaffMobile",
            type: "post",
            async: false,
            data:{cid:cid,mobile:new_mobile,staffId:staffId},
            cache:false,
            dataType: "json",
            timeout: 1000,
            error: function(){ },
            success: function(data){
                console.log(data);
                if(data.status == "200"){
                    $("#new-error").attr("style","display:none;");
                    if(confirm("修改成功！")){
                        /*window.location.reload();*/
                        history.back();
                    }else{
                        window.location.reload();
                    }
                }else{
                    $("#new-error").attr("style","display:block;color:red;");
                    $("#new-error").html("修改失败！");
                }
            }
        });
    }else{
        $("#new-error").attr("style","display:block;color:red;");
        $("#new-error").html(msg);
    }

});